package com.love.marriage.TecentIm.tencent;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.love.marriage.TecentIm.service.ChatService;
import com.love.marriage.TecentIm.service.WuKongImService;
import com.love.marriage.TecentIm.vo.*;
import com.love.marriage.common.vo.Result;
import com.love.marriage.common.vo.ResultBoolean;
import com.love.marriage.userBalanceInfo.vo.ResultUserBalanceInfoVoPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@ApiSort(105)
@Tag(name = "聊天", description = "聊天相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/chat")
public class TencentImController {

    private final ChatService chatService;

    private final WuKongImService wuKongImService;

    @ApiOperationSupport(order = 10, author = "mybatisPlus")
    @Operation(summary = "发送消息", description = "发送消息")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "fromUser", description = "发送人"),
            @Parameter(name = "toUser", description = "接收人"),
            @Parameter(name = "message", description = "消息")
    })
    @PostMapping("/sendMessage")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<String> sendMessage(@RequestParam Long fromUser,
                                      @RequestParam Long toUser,
                                      @RequestParam String message) throws Exception {
        return Result.success(chatService.sendMessage(fromUser, toUser, message));
    }

    @ApiOperationSupport(order = 15, author = "mybatisPlus")
    @Operation(summary = "撤回消息", description = "撤回消息")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "fromAccount", description = "发送人"),
            @Parameter(name = "toAccount", description = "接收人"),
            @Parameter(name = "msgKey", description = "消息key")
    })
    @PostMapping("/withdrawMessage")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<String> withdrawMessage(@RequestParam Long fromAccount,
                                          @RequestParam Long toAccount,
                                          @RequestParam String msgKey) throws Exception {
        return Result.success(chatService.withdrawMessage(fromAccount.toString(), toAccount.toString(), msgKey));
    }

    @ApiOperationSupport(order = 20, author = "mybatisPlus")
    @Operation(summary = "聊天消息列表", description = "聊天消息列表")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultChatListInfoVo.class
                    )))
    @PostMapping("/recentContacts")
    @Tag(name = "聊天", description = "聊天相关接口")
    // TODO: 2024/10/20 添加过滤条件
    public Result<ChatListInfoVo> getRecentContacts(
            @RequestBody RecentContactRequest request) throws Exception {
        return Result.success(chatService.getRecentContacts(request));
    }

    @ApiOperationSupport(order = 30, author = "mybatisPlus")
    @Operation(summary = "聊天详情记录", description = "拉取单聊历史消息")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultUserBalanceInfoVoPage.class
                    )))
    @Parameters({
            @Parameter(name = "operatorUserId", description = "当前操作人"),
            @Parameter(name = "peerUserId", description = "对方"),
            @Parameter(name = "maxCnt", description = "最大数量"),
            @Parameter(name = "lastMsgKey", description = "上一次最后一条消息key", required = false),
    })
    @GetMapping("/getChatHistory")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<ChatEachHistoryVo> getChatHistory(
            @RequestParam(value = "operatorUserId") Long operatorUserId,
            @RequestParam(value = "peerUserId") Long peerUserId,
            @RequestParam(value = "maxCnt") int maxCnt,
            @RequestParam(value = "maxTime") long maxTime,
            @RequestParam(value = "lastMsgKey", required = false) String lastMsgKey) throws Exception {
        return Result.success(chatService.getChatHistory(operatorUserId, peerUserId, maxCnt, maxTime, lastMsgKey));
    }

    @ApiOperationSupport(order = 40, author = "mybatisPlus")
    @Operation(summary = "设置消息已读", description = "设置消息已读")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultBoolean.class
                    )))
    @Parameters({
            @Parameter(name = "fromUser", description = "发送已读回执的用户 UserID", required = true),
            @Parameter(name = "toUser", description = "会话的另一方用户 UserID", required = true),
    })
    @PostMapping("/sendRead")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<Boolean> getChatHistory(
            @RequestParam(value = "fromUser") Long fromUser,
            @RequestParam(value = "toUser") Long toUser,
            @RequestBody List<ChatReadInfoVo> chatReadInfoVoList) throws Exception {
        return Result.success(chatService.sendReadReceipt(fromUser, toUser, chatReadInfoVoList));
    }

    @ApiOperationSupport(order = 50, author = "mybatisPlus")
    @Operation(summary = "删除消息", description = "删除消息")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = ResultBoolean.class
                    )))
    @Parameters({
            @Parameter(name = "fromUser", description = "发送已读回执的用户 UserID", required = true),
            @Parameter(name = "toUser", description = "会话的另一方用户 UserID", required = true),
            @Parameter(name = "msgKeyList", description = "消息key", required = true),
    })
    @PostMapping("/deleteChatMessages")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<Boolean> deleteChatMessages(
            @RequestParam(value = "fromUser") Long fromUser,
            @RequestParam(value = "toUser") Long toUser,
            @RequestParam List<String> msgKeyList) throws Exception {
        return Result.success(chatService.deleteChatMessages(fromUser, toUser, msgKeyList));
    }

    @ApiOperationSupport(order = 60, author = "mybatisPlus")
    @Operation(summary = "获取用户凭证", description = "获取用户凭证")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = true)
    })
    @PostMapping("/generateUserSig")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<String> generateUserSig(
            @RequestParam(value = "userId") Long userId) throws Exception {
        return Result.success(chatService.generateUserSig(userId));
    }

    @ApiOperationSupport(order = 60, author = "mybatisPlus")
    @Operation(summary = "登录本地im", description = "登录本地im")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "用户id", required = true)
    })
    @GetMapping("/loginLocalIm")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<String> loginLocalIm(
            @RequestParam(value = "userId") Long userId) throws Exception {
        return Result.success(wuKongImService.loginOrRegister(userId.toString()).toString());
    }

    @ApiOperationSupport(order = 70, author = "mybatisPlus")
    @Operation(summary = "获取会话记录", description = "获取会话记录")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "发送人id", required = true),
            @Parameter(name = "page", description = "页数", required = false),
            @Parameter(name = "limit", description = "页面大小", required = false)
    })
    @GetMapping("/getLocalImChatList")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<List<LocalImChatListVo>> getLocalImChatList(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "page", required = false) String page,
            @RequestParam(value = "limit", required = false) String limit) throws Exception {
        return Result.success(chatService.getLocalImChatList(userId, page, limit));
    }

    @ApiOperationSupport(order = 70, author = "mybatisPlus")
    @Operation(summary = "获取聊天历史记录", description = "获取聊天历史记录")
    @ApiResponse(responseCode = "200", description = "Success",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = Result.class
                    )))
    @Parameters({
            @Parameter(name = "userId", description = "发送人id", required = true),
            @Parameter(name = "toUserId", description = "接收人id", required = true),
            @Parameter(name = "id", description = "游标", required = false)
    })
    @GetMapping("/getLocalImChatHistory")
    @Tag(name = "聊天", description = "聊天相关接口")
    public Result<LocalImChatHistoryVo> getLocalImChatHistory(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "toUserId") Long toUserId,
            @RequestParam(value = "id", required = false) Long endMessageSeq,
            @RequestParam(value = "limit", required = false) Integer limit) throws Exception {
        return Result.success(chatService.getLocalImChatHistory(userId, toUserId, endMessageSeq, limit));
    }
}
